/*
 * @Author: 葛文杰
 * @Date: 2021/2/2 13:55
 * @Description: egg proxy 报表信息
 */
const { IS_NOT_DELETE, BASE_WHERE_CONDITION_SQL, SQL_DATA_FORMAT } = require('../util/common');

const TABLE_NAME = 'report_info'; // 表名

const TABLE_FIELD = 'id,reportName';

module.exports = {

    /**
     * 获取报表列表
     * @param conn mysql链接
     * @param reportName 报表名称
     * @param pageNo 当前页
     * @param pageSize 分页数量
     * @return {Promise<PermissionStatus>}
     */
    async getReportList(conn, reportName, pageNo, pageSize) {
        let start = (pageNo - 1) * pageSize;
        let selectSql =
          `select ${TABLE_FIELD}, 
           createUser as createUserName,
           DATE_FORMAT(createAt,'${SQL_DATA_FORMAT}') as createAt
           from ${TABLE_NAME}`;
        let conditionSql = `${BASE_WHERE_CONDITION_SQL} and isDelete = ?`;
        let params = [];
        params.push(IS_NOT_DELETE);
        if (reportName) {
            conditionSql = `${conditionSql} and reportName like concat('%',?,'%')`;
            params.push(reportName);
        }
        let sql =
          `${selectSql} ${conditionSql} limit ?,?`;
        params = [ ...params, start, pageSize ];
        let reportList = await conn.query(sql, params);
        return reportList;
    },

    /**
     * 获取查询条件下报表数量
     * @param conn
     * @param reportName
     * @return {Promise<PermissionStatus>}
     */
    async getReportTotal(conn, reportName) {
        let selectSql = `select count(1) as count from ${TABLE_NAME}`;
        let conditionSql = `${BASE_WHERE_CONDITION_SQL} and isDelete = ?`;
        let params = [];
        params.push(IS_NOT_DELETE);
        if (reportName) {
            conditionSql = `${conditionSql} and reportName like concat('%',?,'%')`;
            params.push(reportName);
        }
        let sql = `${selectSql} ${conditionSql}`;
        let [ total ] = await conn.query(sql, params);
        let { count } = total;
        return count;
    },

    /**
     * 获取报表信息
     * @param conn
     * @return {Promise<PermissionStatus>}
     */
    async getReportInfo(conn, reportInfoId) {
        let selectSql = `select * from ${TABLE_NAME}`;
        let conditionSql = `${BASE_WHERE_CONDITION_SQL} and isDelete = ?`;
        let sql = `${selectSql} ${conditionSql}`;
        let params = [ reportInfoId ];
        let reportInfo = await conn.query(sql, params);
        return reportInfo;
    },

    /**
     * 获取同名报表的数量
     * @param conn
     * @param reportName
     * @return {number}
     */
    async countReportNumByName(conn, reportName) {
        let selectSql = `select count(1) as count from ${TABLE_NAME}`;
        let conditionSql = `${BASE_WHERE_CONDITION_SQL} and isDelete = ? and reportName = ?`;
        let params = [ IS_NOT_DELETE, reportName ];
        let sql = `${selectSql} ${conditionSql}`;
        let [ total ] = await conn.query(sql, params);
        let { count } = total;
        return count;
    },

    /**
     * 新增报表信息
     * @param conn
     * @param reportInfo 报表信息
     * @return {Promise<void>}
     */
    async insertReportInfo(conn, reportInfo) {
        let result = await conn.insert(TABLE_NAME, reportInfo);  // 第一步操作
        return result;
    },


    /**
     * 修改报表信息
     * @param conn
     * @param reportInfo 报表信息
     * @return {Promise<void>}
     */
    async updateReportInfo(conn, reportInfo) {
        let result = await conn.update(TABLE_NAME, reportInfo);  // 第一步操作
        return result;
    }
};
